# This test script performs the first of the benchmarks presented in
# the paper about energy measurements that I am submitting to MP2P'10.
# The benchmark performs a sharpening, brightness adjustment, and
# contrast adjustment of the given input image. The input images used
# in the paper can be found in testdata.

from __future__ import with_statement
import sys
import scavenger
from time import sleep, time
from random import random

# Constants.
THUMBNAIL_INTERVAL = 10
CHOOSE_IMAGE_INTERVAL = 30

# Read in the image files.
image_files = ["testdata/butterfly.jpg", "testdata/flower.jpg", "testdata/lappeenranta.jpg"]

images = []
for image_file in image_files:
    with open(image_file, 'rb') as infile:
        image = infile.read()
    with open(image_file.replace('.jpg', '_thumb.jpg'), 'rb') as infile:
        thumb = infile.read()
    images.append((image_file, image, thumb))

# Sleep for a little while to make sure that we discover
# the available surrogates.
sleep(2.0)

@scavenger.aprofilescavenge('len(#0)','len(#0)', store=True)
def sharpen(image, factor):
    from PIL import Image, ImageEnhance
    from StringIO import StringIO
    sio = StringIO(image)
    pil_image = Image.open(sio)
    factor = 1.0 + float(factor)
    new_image = ImageEnhance.Sharpness(pil_image).enhance(factor)
    sio = StringIO()
    new_image.save(sio, pil_image.format, quality=95)
    return sio.getvalue()

@scavenger.aprofilescavenge('len(#0)','len(#0)', store=True)
def brightness(image, factor):
    from PIL import Image, ImageEnhance
    from StringIO import StringIO
    sio = StringIO(image)
    pil_image = Image.open(sio)
    new_image = ImageEnhance.Brightness(pil_image).enhance(factor)
    sio = StringIO()
    new_image.save(sio, pil_image.format, quality=95)
    return sio.getvalue()

@scavenger.aprofilescavenge('len(#0)','len(#0)')
def contrast(image, factor):
    from PIL import Image, ImageEnhance
    from StringIO import StringIO
    sio = StringIO(image)
    pil_image = Image.open(sio)
    new_image = ImageEnhance.Contrast(pil_image).enhance(factor)
    sio = StringIO()
    new_image.save(sio, pil_image.format, quality=95)
    return sio.getvalue()

print '\nTest start:', time()

# Run the test!
for n in range(0, 5):
    for filename, image, thumb in images:
        # Pause period to select a new image.
        sleep(random()*CHOOSE_IMAGE_INTERVAL)

        # Thumbnail processing.
        print
        print filename, '-> thumb (%i)'%n
        try:
            x = sharpen(thumb, 1.0)
            sleep(random()*THUMBNAIL_INTERVAL)
            x = brightness(x, 1.1)
            sleep(random()*THUMBNAIL_INTERVAL)
            contrast(x, 1.1)
            sleep(random()*THUMBNAIL_INTERVAL)
        except Exception, e:
            print 'error (%s)'%e.args,

        # Original image.
        print filename, '-> image (%i)'%n
        try:
            result = sharpen(image, 1.0)
            result = brightness(result, 1.1)
            result = contrast(result, 1.1)
        except Exception, e:
            print 'error (%s)'%e.args,

print '\nTest end:', time()

scavenger.shutdown()
